#include "CFrame.h"

#include "CVMDataTypes.h"
#include "CCoreException.h"
#include "data/CFunction.h"

CFrame::CFrame(char* code, int codeSize, QHash<QString, char*> variables, QVector<char*>& references)
{
    _code = code;
    _codeSize = codeSize;
    _variables = variables;
    _references = &references;
}

CFrame::~CFrame()
{
    //need to copy parameters, when creating the frame
    //for (int i = 0; i < _operands.size(); i++) delete[] _operands[i];
    //for (int i = 0; i < _variables.values().size(); i++) delete[] _variables.values()[i];
}

char* CFrame::code()
{
    return _code;
}

int CFrame::codeSize()
{
    return _codeSize;
}

int& CFrame::pointer()
{
    return _pointer;
}

void CFrame::pushOperand(char* value)
{
    _operands.push(value);
}

char* CFrame::popOperand()
{
    return _operands.pop();
}

char* CFrame::peekOperand()
{
    return _operands.top();
}

void CFrame::copyOperand()
{
    pushOperand(copy(peekOperand()));
}

int CFrame::operandCount()
{
    return _operands.size();
}

void CFrame::pushChar(char value)
{
    char* vl = new char;
    *vl = value;
    pushOperand(vl);
}

void CFrame::pushNull()
{
    pushChar((char)CVMDataTypes::NULL_);
}

void CFrame::pushFalse()
{
    pushChar((char)CVMDataTypes::FALSE);
}

void CFrame::pushTrue()
{
    pushChar((char)CVMDataTypes::TRUE);
}

QHash<QString, char*>& CFrame::variables()
{
    return _variables;
}

QVector<char*>& CFrame::references()
{
    return *_references;
}
